package com.baidu.health.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baidu.health.checkImages.getParameters;
import com.baidu.health.checkImages.sendRequest;
import com.baidu.health.pojo.*;
import com.baidu.health.pojo.resultVo.*;
import com.baidu.health.service.*;
import com.baidu.health.utils.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author 崔先生
 * @version 1.0.0
 * @date 2022/03/25
 * ckassName java.lang.String
 */
@Api(tags = "社区中心")
@RestController
@RequestMapping("/bbs")
public class BbsController {

    //  文章表
    @Autowired
    TbArticleServiceImpl articleService;

    //  关注表
    @Autowired
    TbAttentionServiceImpl attentionService;

    //  收藏表
    @Autowired
    TbCollectServiceImpl collectService;

    //  评论表
    @Autowired
    TbCommentServiceImpl commentService;

    //  转发表
    @Autowired
    TbForwardServiceImpl forwardService;

    //  私信表
    @Autowired
    TbLetterServiceImpl letterService;

    //  点赞表
    @Autowired
    TbLikeServiceImpl likeService;

    //  用户表
    @Autowired
    TbUserServiceImpl userService;

    //  文件管理
    @Autowired
    TbDocumentServiceImpl documentService;

    //  敏感词
    @Autowired
    TbSensitivityServiceImpl sensitivityService;

    @GetMapping("/aaa")
    public void getListsss()throws Exception{
        articleService.getTbSensitivityList();
    }

    /**
     * 同步敏感词
     * @throws Exception 异常
     */
    @ApiOperation("同步敏感词")
    @GetMapping("/syncSensitivity")
    public void syncSensitivity() throws Exception{
        sensitivityService.syncSensitivity();
    }

    /**
     * 同步文章列表数据
     * @throws Exception 异常
     */
    @ApiOperation("同步文章列表数据")
    @GetMapping("/sync")
    public void sync() throws Exception {
        articleService.sync();
    }

    /**
     * 上传文件
     * @param file 文件
     * @return {@link String}
     */
    @ApiOperation("上传文件")
    @PostMapping("/updateFile")
    public uploadImage updateFile(@RequestBody MultipartFile file) throws Exception{
        FileUpload upload = new FileUpload();
        String imgSrc = upload.upload(file);

        String request = new sendRequest().sendRequest(imgSrc);
        JSONObject jsonObject = JSON.parseObject(request);
        String conclusion = (String) jsonObject.get("conclusion");
        if ( conclusion.equals("不合规") ){
            String data = jsonObject.get("data").toString();
            String[] split = data.split(",");
            String[] strings = split[0].split(":");
            String s = strings[1];
            return new uploadImage(null,s,"500");
        }
        return new uploadImage(imgSrc,"上传成功","200");
    }

    /**
     * 得到文章列表
     * @param pageNum  页面num
     * @param pageSize 页面大小
     * @param article  文章
     * @return {@link articleVo}
     * @throws Exception 异常
     */
    @ApiOperation("获取文章列表")
    @PostMapping("/getArticleList/{pageNum}/{pageSize}")
    public articleVo getArticleList(@PathVariable("pageNum")Integer pageNum,@PathVariable("pageSize")Integer pageSize,@RequestBody TbArticle article) throws Exception{
        return articleService.getArticleList(pageNum,pageSize,article);
    }

    /**
     * 得到全部文章
     * @param pageNum  页面num
     * @param pageSize 页面大小
     * @return {@link articleVo}
     */
    @ApiOperation("得到全部文章")
    @PostMapping("/getArticleLists/{pageNum}/{pageSize}")
    public articleVo getArticleLists(@PathVariable("pageNum")Integer pageNum,@PathVariable("pageSize")Integer pageSize)throws Exception{
        return articleService.getArticleLists(pageNum,pageSize);
    }

    /**
     * 获取文章内容
     * @param articleId 文章id
     * @return {@link articleContentVo}
     */
    @ApiOperation("获取文章内容")
    @PostMapping("/getArticleContent/{articleId}")
    public articleContentVo getArticleContent(@PathVariable("articleId")Integer articleId){
        return articleService.getArticleContent(articleId);
    }

    /**
     * 添加文章
     * @param article 文章
     * @return {@link articleVo}
     */
    @ApiOperation("添加文章")
    @PostMapping("/addArticle")
    public articleVo addArticle(@RequestBody TbArticle article) throws Exception{
        return articleService.addArticle(article);
    }


    /**
     * 删除文章
     * @param article 文章
     * @return {@link articleVo}
     */
    @ApiOperation("删除文章")
    @PostMapping("/deleteArticle")
    public articleVo deleteArticle(@RequestBody TbArticle article){
        return articleService.deleteArticle(article);
    }

    /**
     * 更新文章
     * @param article 文章
     * @return {@link articleVo}
     */
    @ApiOperation("更新文章")
    @PostMapping("/updateArticle")
    public articleVo updateArticle(@RequestBody TbArticle article) throws Exception{
        return articleService.updateArticle(article);
    }

    /**
     * 收藏文章
     * @param collect 收藏
     * @return {@link resultVo}
     */
    @ApiOperation("收藏文章")
    @PostMapping("/collectArticle")
    public resultVo collectArticle(@RequestBody TbCollect collect){
        return collectService.collectArticle(collect);
    }

    /**
     * 文章点赞
     * @param like 点赞
     * @return {@link resultVo}
     */
    @ApiOperation("文章点赞")
    @PostMapping("/likeArticle")
    public resultVo likeArticle(@RequestBody TbLike like){
        return likeService.likeArticle(like);
    }

    /**
     * 关注作者
     * @param attention 关注
     * @return {@link resultVo}
     */
    @ApiOperation("关注作者")
    @PostMapping("/attentionWriter")
    public resultVo attentionWriter(@RequestBody TbAttention attention){
        return attentionService.attentionWriter(attention);
    }

    /**
     * 评论文章
     * @param comment 评论
     * @return {@link resultVo}
     */
    @ApiOperation("评论文章")
    @PostMapping("/commentArticle")
    public resultVo commentArticle(@RequestBody TbComment comment)throws Exception{
        return commentService.commentArticle(comment);
    }

    /**
     * 获取子级评论记录
     * @param articleId 文章id
     * @param fatherId  父亲id
     * @return {@link resultVo}
     */
    @ApiOperation("获取子级评论记录")
    @PostMapping("/getChildCommentRecord/{articleId}/{fatherId}")
    public resultVo getChildCommentRecord(@PathVariable("articleId")Integer articleId,@PathVariable("fatherId")Integer fatherId){
        return commentService.getChildCommentRecord(articleId,fatherId);
    }

    /**
     * 获取未审核评论列表
     * @return {@link resultVo}
     */
    @ApiOperation("获取未审核评论列表")
    @PostMapping("/getUnreviewedCommentList/{pageNum}/{pageSize}")
    public articleVo unreviewedCommentList(@PathVariable("pageNum")Integer pageNum,@PathVariable("pageSize")Integer pageSize){
        return commentService.unreviewedCommentList(pageNum,pageSize);
    }

    /**
     * 审核评论
     * @param checkStatus 检查状态
     * @param comment     评论
     * @return {@link resultVo}
     */
    @ApiOperation("审核评论")
    @PostMapping("/checkComment/{checkStatus}")
    public resultVo checkComment(@PathVariable("checkStatus")Integer checkStatus,@RequestBody TbComment comment){
        return commentService.checkComment(checkStatus,comment);
    }

    /**
     * 获取用户信息
     * @param userId 用户id
     * @return {@link userMagess}
     */
    @ApiOperation("获取用户信息")
    @PostMapping("/getUserMessage/{userId}")
    public userMagess getUserMessage(@PathVariable("userId")Integer userId) throws Exception {
        return userService.getUserMessage(userId);
    }

    /**
     * 获取点赞列表
     * @param pageNum  页面num
     * @param pageSize 页面大小
     * @return {@link articleVo}
     */
    @ApiOperation("获取点赞列表")
    @PostMapping("/getLikeList/{pageNum}/{pageSize}")
    public articleVo getLikeList(@PathVariable("pageNum")Integer pageNum,@PathVariable("pageSize")Integer pageSize){
        return likeService.getLikeList(pageNum,pageSize);
    }


    /**
     * 检查文章
     * @param article 文章
     * @return {@link resultVo}
     */
    @ApiOperation("检查文章")
    @PostMapping("/checkArticle/{articleStatus}")
    public resultVo checkArticle(@PathVariable("articleStatus")Integer articleStatus, @RequestBody TbArticle article)throws Exception{
        return articleService.checkArticle(articleStatus,article);
    }

    /**
     * 获取文件列表
     * @return {@link resultVo}
     */
    @ApiOperation("获取文件列表")
    @PostMapping("/getDocumentList")
    public resultVo getDocumentList(){
        return documentService.getDocumentList();
    }

    /**
     * 检查文件
     * @param fileStatus 文件状态
     * @param document   文档
     * @return {@link resultVo}
     */
    @ApiOperation("检查文件")
    @PostMapping("/checkFile/{fileStatus}")
    public resultVo checkFile(@PathVariable("fileStatus")Integer fileStatus,@RequestBody TbDocument document){
        return documentService.checkFile(fileStatus,document);
    }

    /**
     * 获取收藏列表
     * @return {@link resultVo}
     */
    @ApiOperation("获取收藏列表")
    @PostMapping("/getCollectList/{pageNum}/{pageSize}")
    public articleVo getCollectList(@PathVariable("pageNum")Integer pageNum,@PathVariable("pageSize")Integer pageSize){
        return collectService.getCollectList(pageNum,pageSize);
    }

    /**
     * 获取关注列表
     * @return {@link resultVo}
     */
    @ApiOperation("获取关注列表")
    @PostMapping("/getAttentionList/{pageNum}/{pageSize}")
    public articleVo getAttentionList(@PathVariable("pageNum")Integer pageNum,@PathVariable("pageSize")Integer pageSize){
        return attentionService.getAttentionList(pageNum,pageSize);
    }

}


